<?php
/**
 * 收藏
 */
namespace App\Service;

use Phalcon\Di;
use App\Models\EwMemberCollect;
use App\Models\EwHouse;//新房
use App\Models\EwNews;//资讯
use App\Models\EwDeco;//装修
use App\Models\EwData;//数据
use App\Models\EwVideo;//视频
use App\Models\EwDecoComp;//装修公司
use App\Models\EwHouseSeco;//二手房
use App\Models\EwHouseRent;//租房
use App\Models\EwNewsOrig;//原创新闻

class MemberCollectService extends BaseService
{
	const SOURCES_sELF = '觅窝';

	/*const GO_URL_HOUSE = $config->gourl . $config->urls->house;//房源跳转url
	const GO_URL_NEWS  = $config->gourl . $config->urls->news;//资讯跳转url
	const GO_URL_DECO  = $config->gourl . $config->urls->deco;//装修图库跳转url
	const GO_URL_DATA  = $config->gourl . $config->urls->data;//数据跳转url
	const GO_URL_VIDEO = $config->gourl . $config->urls->video;//视频跳转url
	const GO_URL_COMP  = $config->gourl . $config->urls->deco_comp;//装修公司跳转url*/

	public static function getList($params, $count = FALSE)
	{
		$query = EwMemberCollect::query();
		$query->columns(['table_id,list_id as id,json_content,create_time, 1 as is_collect']);

		//member_id
		if (isset($params->member_id) && $params->member_id)
			$query->andwhere("member_id = ".$params->member_id);

		//table_id
		if (isset($params->table_id) && $params->table_id)
			$query->andwhere("table_id = ".$params->table_id);

		//list_id
		if (isset($params->list_id) && $params->list_id)
			$query->andwhere("list_id = ".$params->list_id);

		//总条数
		if ($count)
			return $query->execute()->count();

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by);
			$query->orderBy($sql);
		}

		//分页
		$query->limit($params->page_num,($params->p-1)*$params->page_num);

		return $query->execute();			
	}

	public static function getSearch($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['id,c.member_id,m.nickname,u.mobile,u.account,c.table_id,c.list_id,c.json_content,c.create_time']);
		$builder->addFrom("App\Models\EwMemberCollect", 'c');
		$builder->leftJoin("App\Models\EwMember", 'c.member_id = m.id' , 'm');
		$builder->leftJoin("App\Models\EwUser", 'c.member_id = u.id' , 'u');

		//table_id
		if (isset($params->table_id) && $params->table_id > 0)
			$builder->andwhere("c.table_id = :table_id:")->bind(['table_id' => $params->table_id]);

		//list_id
		if (isset($params->list_id) && $params->list_id)
			$builder->andwhere("c.list_id = :list_id:")->bind(['list_id' => $params->list_id]);

		//title
		if (isset($params->title) && $params->title ){
			$builder->andwhere("c.title LIKE '%" . $params->title . "%'");
		}

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by);
			$builder->orderBy($sql);
		}

		if ($count){
			return $builder->getQuery()->execute()->count();
		}else{
			$builder->limit($params->page_num,($params->p-1)*$params->page_num);
			$result = $builder->getQuery();
			return $result->execute();
		}
	}

	public static function getInfo($id = 0)
	{
		return EwMemberCollect::findFirst($id);
	}

	public static function count()
	{
		return EwMemberCollect::count();
	}

	/**
	 * [getCollectInfo 通过member_id table_id 和 list_id 获取收藏信息 判断是否已经收藏]
	 * @param  [type] $table_id   [description]
	 * @param  [type] $list_id [description]
	 * @return [type]      [description]
	 */
	public static function getCollectInfo($member_id, $table_id ,$list_id)
	{
		return EwMemberCollect::findFirst(['member_id = ' . $member_id . ' AND table_id = ' . $table_id . ' AND list_id = ' . $list_id]);
	}

	/**
	 * [jugePraiseArr 判断是否点赞]
	 * @param  [type]  $member_id [description]
	 * @param  [type]  $table_id  [description]
	 * @param  [type]  $id_arr    [description]
	 * @param  integer $praise    [description]
	 * @return [type]             [description]
	 */
	public static function jugeCollectArr($member_id, $table_id, $list_id_arr)
	{
		$list_id_str = implode($list_id_arr, ',');
		$data = EwMemberCollect::find(['columns' => 'list_id', 'conditions' => 'member_id = ' . $member_id . ' AND table_id = ' . $table_id . ' AND list_id in (' . $list_id_str . ')'])->toArray();
		return array_map('array_shift',$data);
	}


	/**
	 * [getJsonInfo 通过table_id 和 list_id 获取json数据]
	 * @param  [type] $table_id   [description]
	 * @param  [type] $list_id [description]
	 * @return [type]      [description]
	 */
	public static function getJsonInfo($table_id ,$list_id)
	{
		$arr = new \stdClass;
		$arr->name = "house_tags";
		$tags_obj = ConfigsService::getVal($arr);
		$config = Di::getDefault()['config'];
		$default_thumb = $config->imgurl . '/uploads/default.png';

		switch ($table_id) {
			case 1://新房
				if ($data = EwHouse::findFirst($list_id)){

					$data = $data->toArray();

					$data_arr = [];

					// $data_arr['groupId'] = (string)$table_id;
					// $data_arr['id'] = $data['id'];
					// $data_arr['is_collect'] = (string)1;
					$data_arr['is_self'] = isset($data['is_self']) ? $data['is_self'] : (string)0;

					$data_arr['go_url'] = $config->gourl . $config->urls->house . $data['id'];
					$data_arr['three_url'] = $data['three_url'] ? $data['three_url'] : $data_arr['go_url'];

					$tag_arr = explode(",", $data['house_tags']);
					$tag_ar = [];
					foreach ($tag_arr as $tag) {
						if (property_exists($tags_obj, $tag))
							$tag_ar[] = $tags_obj->$tag;
					}
					
					$data_arr['thumb']    	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					// $data_arr['sum'] = (string)1;
					// $data_arr['map_level'] = "house";
					
					$data_arr['name'] 		= $data['name'] ? $data['name'] : '';
					$data_arr['region'] 	= $data['region'] ? $data['region'] : '区域未知';
					$data_arr['address']  	= $data['address'] ? $data['address'] : "地址未知";
					$data_arr['house_tags'] = $data['house_tags'] ? $tag_ar : [];

					$data_arr['price']   	= $data['price'] ? $data['price'] . $data['unit'] : '售价待定';
					$data_arr['area']  		= $data['area'] ? $data['area'] . 'm²' : '面积未知';
					$data_arr['sources']  	= $data['sources'] ? $data['sources'] : self::SOURCES_sELF;					

					// $data_arr['visit']  = $data['visit'];
					$data_arr['lon']  		= $data['lon'] ? $data['lon'] : '0.000000';
					$data_arr['lat']  		= $data['lat'] ? $data['lat'] : '0.000000';
					
					return $data_arr;
				}
				return NULL;
				break;
			
			case 2://新闻
				if ($data = EwNews::findFirst($list_id)){

					$data = $data->toArray();

					$data_arr = [];
					// $data_arr['groupId'] = (string)$table_id;
					// $data_arr['id'] = $data['id'];
					// $data_arr['is_collect'] = (string)1;
					$data_arr['is_self'] 	= isset($data['is_self']) ? $data['is_self'] : (string)0;

					$data_arr['go_url'] 	= $config->gourl . $config->urls->news . $data['id'];
					$data_arr['three_url'] 	= $data['three_url'] ? $data['three_url'] : $data_arr['go_url'];
				
					$data_arr['title'] 		= $data['title'] ? $data['title'] : '';
					$data_arr['tags'] 		= $data['tags'] ? $data['tags'] : '';
					// $data_arr['cont_type'] = $data['cont_type'];
					// $data_arr['cate_name'] = "资讯";
					$data_arr['type_id'] 	= $data['type_id'] ? $data['type_id'] : 0;

					// $data_arr['pics']    = [];
					// $data_arr['album']   = [];
					// $data_arr['num']    = (string)0;
					$data_arr['thumb']  	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					$data_arr['video'] 		= $data['video'] ? $data['video'] : '';
					$data_arr['duration'] 	= $data['duration'] ? $data['duration'] : '';

					$data_arr['sources']  	= $data['sources'] ? $data['sources'] : self::SOURCES_sELF;

					// $data_arr['visit']  = $data['visit'];
					// $data_arr['comment']  = $data['comment'];

					// if ($data['type_id'] == 5){//相册
					// 	$data_arr['album'] 	= json_decode($data['album']);
					// 	$data_arr['num'] 	= (string)count($data_arr['album']);
					// }else{
					// 	$data_arr['pics'] 	= explode(',', $data['pics']);
					// 	$data_arr['pics'] 	= array_filter($data_arr['pics']);
					// 	$data_arr['num'] 	= (string)count($data_arr['pics']);
					// }

					return $data_arr;
				}
				return NULL;
				break;

			case 3://装修图库
				if ($data = EwDeco::findFirst($list_id)){

					$data = $data->toArray();

					$data_arr = [];
					// $data_arr['groupId'] = (string)$table_id;
					// $data_arr['id'] = $data['id'];
					// $data_arr['is_collect'] = (string)1;
					$data_arr['is_self'] 	= isset($data['is_self']) ? $data['is_self'] : (string)0;

					$data_arr['go_url'] 	= $config->gourl . $config->urls->deco . $data['id'];
					$data_arr['three_url'] 	= $data['three_url'] ? $data['three_url'] : $data_arr['go_url'];
					
					$data_arr['title'] 		= $data['title'] ? $data['title'] : '';
					$data_arr['tags'] 		= $data['tags'] ? $data['tags'] : '';
					$data_arr['thumb']  	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					$data_arr['album'] 		= json_decode($data['pics']);
					$data_arr['album'] 		= array_filter($data_arr['album']);
					$data_arr['num'] 		= (string)count($data_arr['pics']);
					
					$data_arr['sources']  = $data['sources'] ? $data['sources'] : self::SOURCES_sELF;

					// $data_arr['visit']  = $data['visit'];
					// $data_arr['comment']  = $data['comment'];
					
					return $data_arr;
				}
				return NULL;
				break;

			case 4://数据
				if ($data = EwData::findFirst($list_id)){

					$data = $data->toArray();

					$data_arr = [];
					// $data_arr['groupId'] = (string)$table_id;
					// $data_arr['id'] = $data['id'];
					// $data_arr['is_collect'] = (string)1;
					$data_arr['is_self'] 	= isset($data['is_self']) ? $data['is_self'] : (string)0;

					$data_arr['go_url'] 	= $config->gourl . $config->urls->data . $data['id'];
					$data_arr['three_url'] 	= $data['three_url'] ? $data['three_url'] : $data_arr['go_url'];

					$data_arr['title'] 		= $data['title'] ? $data['title'] : '';
					$data_arr['tags'] 		= $data['tags'] ? $data['tags'] : '';
					$data_arr['cate_name'] 	= floatval($data['price']) ? "付费数据" : "免费数据";
					$data_arr['thumb']    	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					// $data_arr['pics'] 	   = explode(',', $data['pics']);
					// $data_arr['pics'] 	   = array_filter($data_arr['pics']);
					// $data_arr['num'] 	   = (string)count($data_arr['pics']);
					$data_arr['summary']   	= $data['summary'] ? $data['summary'] : '';
					$data_arr['price'] 	   	= $data['price'] ? $data['price'] : '0.00';
					$data_arr['is_free']   	= floatval($data['price']) ? (string)1 : (string)0;

					$data_arr['sources']  	= $data['sources'] ? $data['sources'] : self::SOURCES_sELF;

					// $data_arr['visit']     = $data['visit'];
					// $data_arr['comment']   = $data['comment'];
					
					return $data_arr;
				}
				return NULL;
				break;

			case 6://视频
				if ($data = EwVideo::findFirst($list_id)){

					$data = $data->toArray();

					$data_arr = [];
					// $data_arr['groupId'] = (string)$table_id;
					// $data_arr['id'] = $data['id'];
					// $data_arr['is_collect'] = (string)1;
					$data_arr['is_self'] 	= isset($data['is_self']) ? $data['is_self'] : (string)0;

					$data_arr['go_url'] 	= $config->gourl . $config->urls->video . $data['id'];
					$data_arr['three_url'] 	= $data['three_url'] ? $data['three_url'] : $data_arr['go_url'];

					$data_arr['title'] 		= $data['title'] ? $data['title'] : '';
					$data_arr['tags'] 		= $data['tags'] ? $data['tags'] : '';
					$data_arr['thumb']  	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					$data_arr['video'] 		= $data['video'] ? $data['video'] : '';
					// $data_arr['type'] 	= $data['type'];
					$data_arr['duration'] 	= $data['duration'] ? $data['duration'] : '';

					$data_arr['sources']  	= $data['sources'] ? $data['sources'] : self::SOURCES_sELF;

					// $data_arr['download']  = $data['download'];
					// $data_arr['share']  = $data['share'];
					// $data_arr['visit']  = $data['visit'];
					// $data_arr['comment']  = $data['comment'];
					
					return $data_arr;
				}
				return NULL;
				break;

			case 7://装修公司
				if ($data = EwDecoComp::findFirst($list_id)){

					$data = $data->toArray();

					$data_arr = [];
					// $data_arr['groupId'] = (string)$table_id;
					// $data_arr['id'] = $data['id'];
					// $data_arr['is_collect'] = (string)1;
					$data_arr['is_self'] 	= isset($data['is_self']) ? $data['is_self'] : (string)0;

					$data_arr['go_url'] 	= $config->gourl . $config->urls->deco_comp . $data['id'];
					$data_arr['three_url'] 	= $data['three_url'] ? $data['three_url'] : $data_arr['go_url'];

					$data_arr['title'] 		= $data['comp_name'] ? $data['comp_name'] : '';
					$data_arr['tags'] 		= $data['tags'] ? $data['tags'] : '';
					$data_arr['thumb']    	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					$data_arr['favor_rate'] = $data['favor_rate'];
					$data_arr['num_rep'] 	= $data['num_rep'];
					$data_arr['address'] 	= $data['address'] ? $data['address'] : '';

					$data_arr['sources']  	= $data['sources'] ? $data['sources'] : self::SOURCES_sELF;

					// $data_arr['visit']  = $data['visit'];
					// $data_arr['comment']  = $data['comment'];
					
					return $data_arr;
				}
				return NULL;
				break;

			case 8://二手房
				if ($data = EwHouseSeco::findFirst($list_id)){

					$data = $data->toArray();

					$data_arr = [];
					// $data_arr['groupId'] = (string)$table_id;
					// $data_arr['id'] = $data['id'];
					// $data_arr['is_collect'] = (string)1;
					$data_arr['is_self'] 	= isset($data['is_self']) ? $data['is_self'] : (string)0;

					$data_arr['go_url'] 	= $config->gourl . $config->urls->house_seco . $data['id'];
					$data_arr['three_url'] 	= $data['three_url'] ? $data['three_url'] : $data_arr['go_url'];

					$tag_arr = explode(",", $data['house_tags']);
					$tag_ar = [];
					foreach ($tag_arr as $tag) {
						if (property_exists($tags_obj, $tag))
							$tag_ar[] = $tags_obj->$tag;
					}
					
					$data_arr['thumb']    	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					// $data_arr['sum'] = (string)1;
					// $data_arr['map_level'] = "house";


					$data_arr['name'] 		= $data['name'] ? $data['name'] : '';
					$data_arr['house_tags'] = $data['house_tags'] ? $tag_ar : [];

					$data_arr['price']   	= $data['price'] ? $data['price'] . '元/平': '售价待定';
					$data_arr['total_price']= $data['total_price'] ? $data['total_price'] . '万/套': '售价待定';
					$data_arr['price']   	= $data['price'] ? $data['price'] . '元/平' : '售价待定';
					$data_arr['region'] 	= $data['region'] ? $data['region'] : '区域未知';
					$data_arr['layout']  	= $data['layout'] ? $data['layout'] : '户型未知';
					$data_arr['area']  		= $data['area'] ? $data['area'] . 'm²' : '面积未知';
					$data_arr['house_name'] = $data['house_name'] ? $data['house_name'] : '';
					$data_arr['address']  	= $data['address'] ? $data['address'] : '';
					$data_arr['house_orien']= $data['house_orien'] ? $data['house_orien'] : '南';

					$data_arr['sources']  	= $data['sources'] ? $data['sources'] : self::SOURCES_sELF;

					// $data_arr['visit']  = $data['visit'];
					$data_arr['lon']  		= $data['lon'] ? $data['lon'] : '0.000000';
					$data_arr['lat']  		= $data['lat'] ? $data['lat'] : '0.000000';
					
					return $data_arr;
				}
				return NULL;
				break;

			case 9://租房
				if ($data = EwHouseRent::findFirst($list_id)){

					$data = $data->toArray();

					$data_arr = [];
					// $data_arr['groupId'] = (string)$table_id;
					// $data_arr['id'] = $data['id'];
					// $data_arr['is_collect'] = (string)1;
					$data_arr['is_self'] 	= isset($data['is_self']) ? $data['is_self'] : (string)0;

					$data_arr['go_url'] 	= $config->gourl . $config->urls->house_rent . $data['id'];
					$data_arr['three_url'] 	= $data['three_url'] ? $data['three_url'] : $data_arr['go_url'];

					$tag_arr = explode(",", $data['house_tags']);
					$tag_ar = [];
					foreach ($tag_arr as $tag) {
						if (property_exists($tags_obj, $tag))
							$tag_ar[] = $tags_obj->$tag;
					}
					
					$data_arr['thumb']    	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					// $data_arr['sum'] = (string)1;
					// $data_arr['map_level'] = "house";

					$data_arr['name'] 		= $data['name'] ? $data['name'] : '';
					$data_arr['house_tags'] = $data['house_tags'] ? $tag_ar : [];

					$data_arr['thumb']    	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					$data_arr['price']   	= $data['price'] ? $data['price'] . $data['unit']: '售价待定';
					$data_arr['region'] 	= $data['region'] ? $data['region'] : '区域未知';
					$data_arr['layout']  	= $data['layout'] ? $data['layout'] : '户型未知';
					$data_arr['area']  		= $data['area'] ? $data['area'] . 'm²' : '面积未知';
					$data_arr['house_name'] = $data['house_name'] ? $data['house_name'] : '';
					$data_arr['house_orien']= $data['house_orien'] ? $data['house_orien'] : '南';

					$data_arr['sources']  	= $data['sources'] ? $data['sources'] : self::SOURCES_sELF;

					// $data_arr['visit']  = $data['visit'];
					$data_arr['lon']  		= $data['lon'] ? $data['lon'] : '0.000000';
					$data_arr['lat']  		= $data['lat'] ? $data['lat'] : '0.000000';
					
					return $data_arr;
				}
				return NULL;
				break;

			case 10://原创
				if ($data = EwNewsOrig::findFirst($list_id)){

					$data = $data->toArray();

					$data_arr = [];
					// $data_arr['groupId'] = (string)$table_id;
					// $data_arr['id'] = $data['id'];
					// $data_arr['is_collect'] = (string)1;
					$data_arr['is_self'] 	= isset($data['is_self']) ? $data['is_self'] : (string)0;

					$data_arr['go_url'] 	= $config->gourl . $config->urls->news_orig . $data['id'];
					$data_arr['three_url'] 	= $data_arr['go_url'];		

					$data_arr['title']    	= $data['title'] ? $data['title'] : '';
					$data_arr['tags']     	= $data['tags'] ? $data['tags'] : '';
					$data_arr['thumb']    	= $data['thumb'] ? $data['thumb'] : $default_thumb;
					// $data_arr['type_id']  = $data['type_id'];
					// $data_arr['topic_id'] = $data['topic_id'];

					$data_arr['sources']  	= $data['sources'] ? $data['sources'] : self::SOURCES_sELF;

					// $data_arr['visit']  = $data['visit'];
					// $data_arr['comment']  = $data['comment'];
					
					return $data_arr;
				}
				return NULL;
				break;

			default:
				return NULL;
				break;
		}
	}

	/**
	 * [save 点击收藏]
	 * @param  [type] $data->table_id [description]
	 * @param  [type] $data->list_id  [description]
	 * @return [type]       		  [description]
	 */
	public static function save($data)
	{
		//判断是否已经收藏
		$info = self::getCollectInfo($data->member_id, $data->table_id, $data->list_id);

		if ($info)
			return FALSE;

		$content = self::getJsonInfo($data->table_id, $data->list_id);

		if($content){
			$arr = new \stdClass();
			$arr->table_id = $data->table_id;
			$arr->list_id  = $data->list_id;
			$arr->member_id = $data->member_id;
			$arr->go_url = $content["go_url"];
			$arr->json_content = json_encode($content);
			$model = new EwMemberCollect();
			return $model->save((array)$arr);
		}
		return NULL;
	}

	/**
	 * [do 收藏 取消收藏]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function do($data)
	{
		//判断是否已经收藏
		$info = self::getCollectInfo($data->member_id, $data->table_id, $data->list_id);

		if ($info && $info->delete()){//取消收藏
			return "delete";
		}else{
			$content = self::getJsonInfo($data->table_id, $data->list_id);

			if($content){//点击收藏
				$arr = new \stdClass();
				$arr->table_id = $data->table_id;
				$arr->list_id  = $data->list_id;
				$arr->member_id = $data->member_id;
				$arr->go_url = $content["go_url"];
				$arr->json_content = json_encode($content);

				$model = new EwMemberCollect();
				$model->save((array)$arr);
				return "save";
			}
		}
	}

	/**
	 * [delete 彻底删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwMemberCollect::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}
}